﻿#include "SideBrowser.h"
#include "styleDefine.h"

#define SIDEWIDGET_WIDTH        900
#define SIDEWIDGET_HEIGHT       700

#define SIDEBAR_MAX_WIDTH       200 // 侧边栏最大显示时宽度
#define SIDEBAR_MIN_WIDTH       45  // 侧边栏最小显示时宽度（与高度相同，最小时正方形，需加上状态宽度）
#define SIDEITME_HEIGHT         40  // 侧边栏单个模块高度
#define ONEITEM_STATUS_WIDTH    5   // 选择状态模块宽度


OneItem::OneItem(const QString& strTitle, const QString& strIcon, const uint nIndex, QWidget* parent)
    : QWidget(parent)
    , m_pStatus(nullptr)
    , m_pLogo(nullptr)
    , m_pTitle(nullptr)
    , m_bFresh(false)
    , m_strIcon(strIcon)
    , m_strTitle(strTitle)
    , m_nIndex(nIndex)
{
    setMouseTracking(true); // 开启鼠标追踪，以便捕获鼠标悬停事件
}

// 首次使用，必须刷新
void OneItem::refresh()
{
    //qDebug() << "OneItem "<<  m_bFresh << m_nIndex << this->rect();
    if(false == m_bFresh)
    {
        const QRect srcRect = this->rect();
        const QPixmap pixmap(m_strIcon);

        m_pStatus = new QLabel(this);
        m_pStatus->setGeometry(0, ONEITEM_STATUS_WIDTH, ONEITEM_STATUS_WIDTH, srcRect.height() - 2 * ONEITEM_STATUS_WIDTH);
        m_pStatus->setStyleSheet(TOOL_COMMON_BACKGROUND_COLOUR);

        m_pLogo = new QLabel(this);
        m_pLogo->setGeometry(ONEITEM_STATUS_WIDTH, 0, srcRect.height(), srcRect.height());
        m_pLogo->setPixmap(pixmap);
        m_pLogo->setScaledContents(true);   // 设置图标自适应
        m_pLogo->setFixedSize(QSize(srcRect.height() - 5, srcRect.height() - 5));

        m_pTitle = new QLabel(this);
        m_pTitle->setGeometry(srcRect.height() + ONEITEM_STATUS_WIDTH, 0, srcRect.width() - srcRect.height()- ONEITEM_STATUS_WIDTH, srcRect.height());
        m_pTitle->setText(m_strTitle);
        m_pTitle->setFont(gFontConsolas);

        this->setStyleSheet(TOOL_COMMON_BACKGROUND_COLOUR);
        m_bFresh = true;
    }
}

void OneItem::setNoSelectStatus()
{
    m_pStatus->setStyleSheet(TOOL_COMMON_BACKGROUND_COLOUR);
}

void OneItem::setSelectedStatus()
{
    m_pStatus->setStyleSheet(TOOL_COMMON_SELECTED_COLOUR);
}

void OneItem::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        // qDebug() << "左键点击坐标：" << event->pos();
        setSelectedStatus();
        emit signal_selected(m_nIndex, m_strTitle);
    }
    else if (event->button() == Qt::RightButton)
    {
        //qDebug() << "右键点击坐标：" << event->pos();
    }
}

bool OneItem::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type() == QEvent::Enter)
    {
        // 鼠标进入事件
        this->setStyleSheet(TOOL_COMMON_POSENTER_COLOUR);
    }
    else if (event->type() == QEvent::Leave)
    {
        // 鼠标离开事件
        this->setStyleSheet(TOOL_COMMON_BACKGROUND_COLOUR);
    }

    return QWidget::eventFilter(obj, event);
}


/*************************************************/
SideBar::SideBar(QWidget* parent)
    : QFrame(parent)
    , m_pSideSpace(nullptr)
{
    m_mpItems.clear();
    m_pSideSpace = new SideSpace(this);
}

void SideBar::setBaseBackground(const QString strColour)
{
    setStyleSheet(strColour);
    m_strBaseBackground = strColour;
}

// 首次使用，必须刷新
void SideBar::refresh()
{
    //qDebug() << "refresh Size：" << m_mpItems.size();
    for (const auto& it : m_mpItems)
    {
        it.second->refresh();
    }

    m_pSideSpace->setStyleSheet(m_strBaseBackground);
    m_pSideSpace->setGeometry(0, m_mpItems.size() * SIDEITME_HEIGHT, SIDEBAR_MAX_WIDTH, this->rect().height() - m_mpItems.size() * SIDEITME_HEIGHT);
}

void SideBar::addTabPage(const QString &strTitle, const QString &strIcon)
{
    //qDebug() << "bar >>>>>>>>>>" << this->rect();
    const uint nSum = m_mpItems.size();
    OneItem* pItem = new OneItem(strTitle, strIcon, nSum, this);
    pItem->setGeometry(0, SIDEITME_HEIGHT * nSum, SIDEBAR_MAX_WIDTH, SIDEITME_HEIGHT);
    pItem->installEventFilter(pItem); // 安装事件过滤器
    connect(pItem, SIGNAL(signal_selected(uint, QString)), this, SLOT(slt_selected(uint, QString)));
    m_mpItems.insert(std::make_pair(nSum, pItem));
}

bool SideBar::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::Enter)
    {
        // 鼠标进入事件
        emit eventEnterStatus(false);
    }
    else if (event->type() == QEvent::Leave)
    {
        // 鼠标离开事件
        emit eventEnterStatus(true);
    }

    return QWidget::eventFilter(obj, event);
}

void SideBar::slt_selected(uint nIndex, QString strTitle)
{
    //qDebug() << "被选中标签页 " << nIndex;
    for(const auto &it : m_mpItems)
    {
        if(nIndex != it.first)
        {
            it.second->setNoSelectStatus();
        }
    }

    emit itemSelected(nIndex, strTitle);
}

/***********************************************************/
SideBrowser::SideBrowser(QWidget* parent)
    : QFrame(parent)
    , m_pAnimationBar(nullptr)
    , m_pAnimationTab(nullptr)
    , m_pSidebar(nullptr)
    , m_pTabBrowser(nullptr)
    , m_SideWidth(SIDEBAR_MAX_WIDTH)
    , m_bExpandStatus(true)
    , m_bAutoExpand(false)
{
    this->setMinimumSize(SIDEWIDGET_WIDTH, SIDEWIDGET_HEIGHT);
    m_pTabBrowser = new QStackedWidget(this);
    m_pSidebar = new SideBar(this);
    m_pSidebar->installEventFilter(m_pSidebar); // 安装事件过滤器
    m_pSidebar->setBaseBackground(TOOL_COMMON_BACKGROUND_COLOUR);
    connect(m_pSidebar, SIGNAL(eventEnterStatus(bool)), this, SLOT(slt_eventEnterStatus(bool)));
    connect(m_pSidebar, SIGNAL(itemSelected(uint, QString)), this, SLOT(slt_selected(uint, QString)));

    // 设置默认状态为收起
    m_bExpandStatus = false;
    if(true == m_bExpandStatus)
    {
        m_SideWidth = SIDEBAR_MAX_WIDTH;
    }
    else
    {
        m_SideWidth = SIDEBAR_MIN_WIDTH;
    }

    // 设置模块位置
    m_pSidebar->setGeometry(0, 0, m_SideWidth, SIDEWIDGET_HEIGHT);
    m_pTabBrowser->setGeometry(m_SideWidth, 0, SIDEWIDGET_WIDTH - m_SideWidth, SIDEWIDGET_HEIGHT);

    // 添加动画操作
    m_pAnimationBar = new QPropertyAnimation(m_pSidebar, "geometry");
    m_pAnimationBar->setDuration(ANIMATION_DURATION_TIME);
    m_pAnimationTab = new QPropertyAnimation(m_pTabBrowser, "geometry");
    m_pAnimationTab->setDuration(ANIMATION_DURATION_TIME);
}

void SideBrowser::changeSideBar()
{
    //m_bAutoExpand = !m_bAutoExpand;   // 暂不开启自动展开
    if(true == m_bExpandStatus)
    {
        collapsed();
    }
    else
    {
        expand();
    }
}

void SideBrowser::addTabPage(const QString& strTitle, const QString& strIcon, QWidget* pTab)
{
    m_pSidebar->addTabPage(strTitle, strIcon);
    m_pTabBrowser->addWidget(pTab);
}

void SideBrowser::addTabPage(const QString& strTitle, const QString& strIcon, const FUNC_FORM_TYPE nType)
{
    m_pSidebar->addTabPage(strTitle, strIcon);
    CustomTabView* pTabView = new CustomTabView(this);
    pTabView->addTabType(nType);
    m_pTabBrowser->addWidget(pTabView);
}

void SideBrowser::clostAutoExpand()
{
    m_bAutoExpand = false;
}

// 设置子界面的标题栏是否显示
void SideBrowser::showTabIndex()
{
    CustomTabView* pTabView = dynamic_cast<CustomTabView*>(m_pTabBrowser->currentWidget());
    if (nullptr != pTabView)
    {
        pTabView->showTabIndex();
    }
}

void SideBrowser::expand()
{
    if(true == m_bExpandStatus)
    {
        // 当前状态为已展开，无需操作，返回
        return;
    }

    const QRect srcSize = this->rect();
    m_pAnimationBar->setStartValue(QRect(0,0, SIDEBAR_MIN_WIDTH, srcSize.height()));
    m_pAnimationBar->setEndValue(QRect(0, 0, SIDEBAR_MAX_WIDTH, srcSize.height()));

    m_pAnimationTab->setStartValue(QRect(SIDEBAR_MIN_WIDTH, 0, srcSize.width() - SIDEBAR_MIN_WIDTH, srcSize.height()));
    m_pAnimationTab->setEndValue(QRect(SIDEBAR_MAX_WIDTH, 0, srcSize.width() - SIDEBAR_MAX_WIDTH, srcSize.height()));
    m_SideWidth = SIDEBAR_MAX_WIDTH;
    m_bExpandStatus = true;
    m_pAnimationBar->start();
    m_pAnimationTab->start();
}

void SideBrowser::collapsed()
{
    if(false == m_bExpandStatus)
    {
        // 当前状态为已收起，无需操作，返回
        return;
    }

    const QRect srcSize = this->rect();
    m_pAnimationBar->setStartValue(QRect(0,0, SIDEBAR_MAX_WIDTH, srcSize.height()));
    m_pAnimationBar->setEndValue(QRect(0, 0, SIDEBAR_MIN_WIDTH, srcSize.height()));

    m_pAnimationTab->setStartValue(QRect(SIDEBAR_MAX_WIDTH, 0, srcSize.width() - SIDEBAR_MAX_WIDTH, srcSize.height()));
    m_pAnimationTab->setEndValue(QRect(SIDEBAR_MIN_WIDTH, 0, srcSize.width() - SIDEBAR_MIN_WIDTH, srcSize.height()));
    m_SideWidth = SIDEBAR_MIN_WIDTH;
    m_bExpandStatus = false;
    m_pAnimationBar->start();
    m_pAnimationTab->start();
}

void SideBrowser::resizeEvent(QResizeEvent *event)
{
    //qDebug() << "窗口大小变为：" << event->size();
    m_pSidebar->setGeometry(0, 0, m_SideWidth, event->size().height());
    m_pTabBrowser->setGeometry(m_SideWidth, 0, event->size().width() - m_SideWidth, event->size().height());
    m_pSidebar->refresh();
}

void SideBrowser::slt_eventEnterStatus(bool bStatus)
{
   // qDebug() << bStatus << m_bAutoExpand;
    if(false == m_bAutoExpand)
    {
        return;
    }

    if(true == bStatus)
    {
        collapsed();
    }
    else
    {
        expand();
    }
}

void SideBrowser::slt_selected(uint nIndex, QString strTitle)
{
    m_pTabBrowser->setCurrentIndex(nIndex);
    emit showPageTitle(strTitle);
}